package com.imessage.service.impl;

import com.bean.BaseProduct;
import com.bean.BusinessWuma;
import com.dd.plist.NSDictionary;
import com.dd.plist.NSObject;
import com.dd.plist.PropertyListParser;
import com.imessage.service.BaseService;
import com.imessage.service.WumaService;
import com.imessage.type.BusinessAppleState;
import com.imessage.type.BusinessWumaState;
import com.imessage.utils.ByteUtil;
import com.imessage.utils.StrUtil;
import io.ebean.Expr;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import java.io.File;
import java.util.*;

@Slf4j
@Service
public class WumaServiceImpl implements WumaService {

    @Autowired
    private BaseService baseService;
    @Autowired
    protected Environment environment;

    @Override
    public void createConfig() {
        List<Expression> expressionList = new ArrayList<Expression>();
        expressionList.add(Expr.eq("state", BusinessWumaState.init.getCode()));
        List<BusinessWuma> businessWumaList=baseService.findList(BusinessWuma.class, expressionList);
        if(businessWumaList==null||businessWumaList.size()==0){
            return;
        }
        String path=String.format("%s%s%s",
                StrUtil.getOsResourceLoc(environment),File.separator,"config");
        String configFile=path+ File.separator+"config.plist";
        for(BusinessWuma businessWuma:businessWumaList){
            BaseProduct baseProduct= baseService.findObject(
                    BaseProduct.class, Expr.eq("name", businessWuma.getProductName()));
            if(baseProduct==null){
                continue;
            }
            try{
                NSDictionary rootDict = (NSDictionary)PropertyListParser.parse(configFile);
                modifyConfig(rootDict,businessWuma,baseProduct);
                businessWuma.setConfigData(rootDict.toXMLPropertyList());
            }catch (Exception e){
                log.error("", e);
            }
            businessWuma.setState(BusinessWumaState.available);
            baseService.updateObject(businessWuma);
        }
    }

    private void modifyConfig(NSDictionary rootDict,
                              BusinessWuma businessWuma, BaseProduct baseProduct){
        Set<String> rootSet= rootDict.keySet();
        Iterator rootIterator=rootSet.iterator();
        String smuuid=UUID.randomUUID().toString().toUpperCase();
        while(rootIterator.hasNext()){
            String key=(String)rootIterator.next();
            if(key.equalsIgnoreCase("smbios")){
                NSDictionary smbiosDictionary=(NSDictionary)rootDict.get(key);
                Set<String> smbiosSet= smbiosDictionary.keySet();
                Iterator smbiosIterator=smbiosSet.iterator();
                while(smbiosIterator.hasNext()){
                    String smbiosKey=(String)smbiosIterator.next();
                    if(smbiosKey.equalsIgnoreCase("productname")){
                        smbiosDictionary.put(smbiosKey, baseProduct.getName());
                    }
                    if(smbiosKey.equalsIgnoreCase("family")){
                        smbiosDictionary.put(smbiosKey, baseProduct.getFamily());
                    }
                    if(smbiosKey.equalsIgnoreCase("boardmanufacturer")){
                        smbiosDictionary.put(smbiosKey, baseProduct.getManufacturer());
                    }
                    if(smbiosKey.equalsIgnoreCase("biosversion")){
                        smbiosDictionary.put(smbiosKey, baseProduct.getBiosVersion());
                    }
                    if(smbiosKey.equalsIgnoreCase("biosreleasedate")){
                        smbiosDictionary.put(smbiosKey, baseProduct.getBiosDate());
                    }
                    if(smbiosKey.equalsIgnoreCase("biosvendor")){
                        smbiosDictionary.put(smbiosKey, baseProduct.getBiosVendor());
                    }
                    if(smbiosKey.equalsIgnoreCase("version")){
                        smbiosDictionary.put(smbiosKey, baseProduct.getSystemVersion());
                    }
                    if(smbiosKey.equalsIgnoreCase("chassismanufacturer")){
                        smbiosDictionary.put(smbiosKey, baseProduct.getChassisManufacturer());
                    }
                    if(smbiosKey.equalsIgnoreCase("locationinchassis")){
                        smbiosDictionary.put(smbiosKey, baseProduct.getLocationInChassis());
                    }
                    if(smbiosKey.equalsIgnoreCase("chassisassettag")){
                        smbiosDictionary.put(smbiosKey, baseProduct.getChassisAssetTag());
                    }
                    if(smbiosKey.equalsIgnoreCase("chassistype")){
                        smbiosDictionary.put(smbiosKey, baseProduct.getChassisType());
                    }
                    if(smbiosKey.equalsIgnoreCase("boardtype")){
                        smbiosDictionary.put(smbiosKey, baseProduct.getBoardType());
                    }
                    if(smbiosKey.equalsIgnoreCase("boardserialnumber")){
                        smbiosDictionary.put(smbiosKey, businessWuma.getMlb());
                    }
                    if(smbiosKey.equalsIgnoreCase("serialnumber")){
                        smbiosDictionary.put(smbiosKey, businessWuma.getSerialNum());
                    }
                    if(smbiosKey.equalsIgnoreCase("board-id")){
                        smbiosDictionary.put(smbiosKey, businessWuma.getBoardId());
                    }
                    if(smbiosKey.equalsIgnoreCase("smuuid")){
                        smbiosDictionary.put(smbiosKey, smuuid);
                    }
                }
            }
            if(key.equalsIgnoreCase("systemparameters")){
                NSDictionary systemDictionary=(NSDictionary)rootDict.get(key);
                Set<String> systemSet= systemDictionary.keySet();
                Iterator systemIterator=systemSet.iterator();
                while(systemIterator.hasNext()) {
                    String systemKey = (String) systemIterator.next();
                    if (systemKey.equalsIgnoreCase("customuuid")) {
                        systemDictionary.put(systemKey, smuuid);
                    }
                }
            }

            if(key.equalsIgnoreCase("RtVariables".toLowerCase())){
                NSDictionary systemDictionary=(NSDictionary)rootDict.get(key);
                Set<String> systemSet= systemDictionary.keySet();
                Iterator systemIterator=systemSet.iterator();
                while(systemIterator.hasNext()) {
                    String systemKey = (String) systemIterator.next();
                    if (systemKey.equalsIgnoreCase("mlb")) {
                        systemDictionary.put(systemKey, businessWuma.getMlb());
                    }
                }
            }
        }
    }

    @Override
    public BusinessWuma findBusinessWuma(String mac) {
        List<Expression> expressionList = new ArrayList<Expression>();
        expressionList.add(Expr.eq("mac", mac));
        return baseService.findObject(BusinessWuma.class, expressionList);
    }

    @Override
    public BusinessWuma findBusinessWumaByRandom() {
        List<Expression> expressionList=new ArrayList<Expression>();
        expressionList.add(Expr.eq("state", BusinessAppleState.available.getCode()));
        return baseService.findObject(BusinessWuma.class,expressionList,null,"random()" );
    }
}
